<template>
    <div class="elements-list">
        <h1>支出类 - {{ title }}</h1>
        <div class="item-list">
            <div class="item" @click="openAdditionDialog" v-show="!editState">
                <el-icon>
                    <Plus />
                </el-icon>
                新增
            </div>
            <div v-for="element in elementList" class="item">
                {{ element.typeName }}
                <input type="checkbox" v-model="operatingList" :value="element.id" v-if="element.status" v-show="editState" class="checkbox">
            </div>
        </div>
        <!-- 编辑按钮 -->
        <span class="edit" v-show="!editState" @click="editElements">
            <el-icon>
                <Edit />
            </el-icon>
            编辑
        </span>
        <!-- 删除按钮 -->
        <span class="edit" v-show="editState" @click="openDeleteDialog">
            <el-icon>
                <Delete />
            </el-icon>
            删除
        </span>
        <span class="cancel" v-show="editState" @click="cancelEdit">取消</span>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog v-model="isShowAddition" title="请输入要新增的分类名称" width="500">
        <el-input v-model="newType"></el-input>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="isShowAddition = false">取消</el-button>
                <el-button type="primary" @click="addElement">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog v-model="isShowDelete" title="确定要删除选中的分类吗？" width="500">
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="isShowDelete = false">取消</el-button>
                <el-button type="primary" @click="deleteItems">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import { SecondLvExpenseType } from '@/types/expense';
import { addExpenseType, deleteExpenseTypes } from '@/api/expense';
import useFinanceStore from '@/store/modules/finance';
const financeStore = useFinanceStore()
const props = defineProps({
    elementList:{
        type:Object as PropType<SecondLvExpenseType[]>,
        required:true
    },
    title:{
        type:String,
        required:true
    },
    label1:{
        type:Number,
        required:true
    }
})
let editState = ref(false)
let isShowAddition = ref(false)
let isShowDelete = ref(false)
let newType = ref('')
let operatingList = ref([])
const openAdditionDialog = () => {
    isShowAddition.value = true
}
const addElement = async () => {
    await addExpenseType(newType.value,props.label1)
    financeStore.getExpenseTypes()
    isShowAddition.value = false
}
const editElements = () => {
    editState.value = true
}
const cancelEdit = () => {
    operatingList.value = []
    editState.value = false
}
const deleteItems = async () => {
    if (!operatingList.value.length)
        ElMessage({
            message: '请选择至少一项元素',
            type: 'error'
        })
    else {
        // 删除，刷新，取消编辑
        await deleteExpenseTypes(operatingList.value)
        financeStore.getExpenseTypes()
        isShowDelete.value = false
        editState.value = false
    }
}
const openDeleteDialog = () => {
    isShowDelete.value = true
}
onMounted(() => {
})
</script>
<style scoped lang="scss">
.elements-list {
    position: relative;
    width: 350px;
    height: 270px;
    margin-left: 30px;
    margin-top: 20px;
    border-radius: 10px;
    border: 1px solid rgb(0, 200, 255);
    box-shadow: 5px 5px 5px rgb(169, 169, 169);
    overflow: auto;

    h1 {
        margin: 10px 0;
        text-align: center;
    }

    .item-list {
        display: flex;
        flex-wrap: wrap;

        .item {
            position: relative;
            width: 100px;
            height: 60px;
            margin-left: 10px;
            margin-top: 10px;
            line-height: 60px;
            text-align: center;
            border: 1px solid rgb(0, 221, 255);
            border-radius: 5px;
            cursor: pointer;

            :deep(.checkbox) {
                position: absolute;
                right: -5px;
                top: -10px;
                width: 20px;
                height: 20px;
            }
        }
    }

    .edit {
        position: absolute;
        right: 20px;
        bottom: 10px;
        cursor: pointer;
    }

    .cancel {
        position: absolute;
        left: 20px;
        bottom: 10px;
        cursor: pointer;
    }
}
</style>